package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.service.DynamicService;
import com.wing.purchase.dao.PurchaseWarehousingPlanDao;
import com.wing.purchase.model.entity.PurchaseSignForProduct;
import com.wing.purchase.model.entity.PurchaseWarehousingPlan;
import com.wing.purchase.model.entity.PurchaseWarehousingPlanProduct;
import com.wing.purchase.model.request.PurchaseWarehousingPlanForm;
import com.wing.purchase.model.response.PurchaseWarehousingPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购入库计划-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchaseWarehousingPlanServiceImpl extends ServiceImpl<PurchaseWarehousingPlanDao, PurchaseWarehousingPlan> implements PurchaseWarehousingPlanService {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private PurchaseWarehousingPlanProductService purchaseWarehousingPlanProductService;

    @Override
    public int add(PurchaseWarehousingPlanForm purchaseWarehousingPlanForm) {
        //校验
        PurchaseWarehousingPlan purchaseWarehousingPlan =new PurchaseWarehousingPlan();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchaseWarehousingPlan.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseWarehousingPlanForm, purchaseWarehousingPlan, copyOptions);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code = dynamicService.getMaxCode("plan_no", "pur_purchase_warehousing_plan");
        String day = simpleDateFormat.format(new Date());
        purchaseWarehousingPlan.setPlanNo("PW"+day+code);
        int i = baseMapper.insert(purchaseWarehousingPlan);
        if(i > 0){
            for(PurchaseWarehousingPlanProduct purchaseWarehousingPlanProduct : purchaseWarehousingPlanForm.getPurchaseWarehousingPlanProducts()){
                purchaseWarehousingPlanProduct.setPurchaseWarehousingPlanId(purchaseWarehousingPlan.getId());
            }
            purchaseWarehousingPlanProductService.saveBatch(purchaseWarehousingPlanForm.getPurchaseWarehousingPlanProducts());
        }
        return i;
    }

    @Override
    public List<PurchaseWarehousingPlanVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchaseWarehousingPlanVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchaseWarehousingPlanVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseWarehousingPlanVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
